using System.Collections.Generic;
using UnityEngine;

public class BallStorageRack : MonoBehaviour
{
	[SerializeField]
	private float m_BallMaxSpeed;

	[SerializeField]
	private float m_BallInitForceStrength;

	[SerializeField]
	private Transform m_InitTrans;

	[SerializeField]
	private Transform m_ForcePointTrans;

	private float m_Time;

	[SerializeField]
	private float m_TimeInterval = 1f;

	private Queue<Rigidbody> m_BallQueue = new Queue<Rigidbody>();

	private void Awake()
	{
		m_Time = m_TimeInterval;
	}

	private void Update()
	{
		if (m_BallQueue.Count <= 0)
		{
			return;
		}
		m_Time -= Time.deltaTime;
		if (m_Time < 0f)
		{
			Rigidbody rigidbody = m_BallQueue.Dequeue();
			WhiteBall component;
			if (!(component = rigidbody.GetComponent<WhiteBall>()) || component.BallState == PoolBall.State.POTTED)
			{
				rigidbody.GetComponent<PoolBall>().LightRenderer.Open();
				rigidbody.GetComponent<Collider>().enabled = true;
				rigidbody.isKinematic = false;
				rigidbody.useGravity = true;
				rigidbody.velocity = (m_ForcePointTrans.position - m_InitTrans.position).normalized * m_BallInitForceStrength;
				rigidbody.GetComponent<Renderer>().enabled = true;
				rigidbody.gameObject.AddComponent<RackBallCollision>();
				PhysicalSupportTools.MaxSpeedLimitTo(rigidbody.gameObject, m_BallMaxSpeed);
				m_Time = m_TimeInterval;
			}
		}
	}

	public void Add(PoolBall ball)
	{
		ball.AudioEnable = true;
		ball.LightRenderer.Close();
		Rigidbody component = ball.GetComponent<Rigidbody>();
		component.position = m_InitTrans.position;
		component.velocity = Vector3.zero;
		component.angularVelocity = Vector3.zero;
		component.useGravity = false;
		component.isKinematic = true;
		component.GetComponent<Collider>().enabled = false;
		component.GetComponent<Renderer>().enabled = false;
		m_BallQueue.Enqueue(component);
	}

	public void Remove(PoolBall ball)
	{
		ball.Reset();
	}

	public void OnDrawGizmos()
	{
		if (Application.isPlaying)
		{
			Gizmos.color = ((m_BallQueue.Count <= 0) ? Color.red : Color.green);
			Gizmos.DrawCube(base.transform.position, Vector3.one * 0.2f);
		}
	}
}
